/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyectoTiberio.interfaz;

import com.proyectoTiberio.interfaz.utility.ParametroSolicitud;
import com.proyectoTiberio.interfaz.utility.TipoSolicitud;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.cookie.Cookie;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
 *
 * @author Chema
 */
public class servicioHttp {

    String idSesion = "";
    Date expiraSesion;

    public boolean conectar() {
        try {
            
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpost = new HttpPost("https://alliances.commandandconquer.com/j_security_check");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("j_username", "050686c@gmail.com"));
            nvps.add(new BasicNameValuePair("j_password", "05junio06"));


            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse response = httpclient.execute(httpost);
            
            System.out.println("Post logon cookies:");
            List<org.apache.http.cookie.Cookie> cookies = httpclient.getCookieStore().getCookies();
            
            if (cookies.isEmpty()) {
                
                System.out.println("No se ha recibido ninguna cookie");
                expiraSesion = null;
                
                return false;
                
                
            } else {
                
                for (Cookie cookie : cookies) 
                {
                   
                    System.out.println("cookie->"+ cookie );
                    if("JSESSIONID".equals(cookie.getName()))
                   {
                       idSesion = cookie.getValue();
                      return abrirSesion();
                   }
                }
                
                System.out.println("NO se ha recibido ninguna cookie con idSesion");
                return false;
            }

        } catch (Exception e) {
            System.out.println(e);
            return false;
        }

   






    }
    
    private boolean abrirSesion() 
    {
        
         //https://prodgame12.alliances.commandandconquer.com/49/Presentation/Service.svc/ajaxEndpoint/OpenSession?%7B%22session%22%3A%22c04ee83e-1017-49a5-9ed3-249ee2774cd8%22%2C%22reset%22%3Atrue%2C%22refId%22%3A1340921531048%2C%22version%22%3A-1%7D=
                       
         //https://prodgame12.alliances.commandandconquer.com/49/Presentation/Service.svc/ajaxEndpoint/OpenSession?%7B%22session%22%3A%22c04ee83e-1017-49a5-9ed3-249ee2774cd8%22%2C%22reset%22%3Atrue%2C%22refId%22%3A1340921531048%2C%22version%22%3A-1%7D=
                       
        try
        {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpost = new HttpPost("https://prodgame12.alliances.commandandconquer.com/49/Presentation/Service.svc/ajaxEndpoint/OpenSession");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("j_username", "050686c@gmail.com"));
            nvps.add(new BasicNameValuePair("j_password", "05junio06"));


            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse response = httpclient.execute(httpost);
            
            System.out.println("Post logon cookies:");
            List<org.apache.http.cookie.Cookie> cookies = httpclient.getCookieStore().getCookies();
            
            if (cookies.isEmpty()) {
                
                System.out.println("No se ha recibido ninguna cookie");
                expiraSesion = null;
                
                return false;
                
                
            } else {
                
                for (Cookie cookie : cookies) 
                {
                   if("JSESSIONID".equals(cookie.getName()))
                   {
                       idSesion = cookie.getValue();
                      return abrirSesion();
                   }
                }
                
                System.out.println("NO se ha recibido ninguna cookie con idSesion");
                return false;
            }

        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    
    }
}

////https://prodgame12.alliances.commandandconquer.com/49/Presentation/Service.svc/ajaxEndpoint/OpenSession       ?%7B%22session%22%3A%22c04ee83e-1017-49a5-9ed3-249ee2774cd8%22%2C%22reset%22%3Atrue%2C%22refId%22%3A1340921531048%2C%22version%22%3A-1%7D=
//https://prodgame12.alliances.commandandconquer.com/49/Presentation/Service.svc/ajaxEndpoint/NotificationGetRange?%7B%22session%22%3A%2265053e1a-e472-49ea-bbdd-2526fc2f8b98%22%2C%22category%22%3A0%2C%22skip%22%3A0%2C%22take%22%3A50%2C%22sortOrder%22%3A1%2C%22ascending%22%3Afalse%7D=